<?php
include_once('../../php/postgre.php');
$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];

if ($act == 'simpan_visi'){
	$idvisi = $_POST['id_visi'];
	$visi = $_POST['visi'];
	$periode = $_POST['periode'];
	
	if(!is_numeric($idvisi)){
		echo "Err: invalid id.";
		exit;
	}
	
	if ($idvisi>0){
		exec_query("update rpjm_visi set visi='".$visi."' ,periode='".$periode."' where id_visi=".$idvisi);
		echo 'success__';
		logs('Mengubah Visi periode '.$periode,'Visi Baru = '.$visi);
	}else{
		$maxid = get_maxid('id_visi', 'rpjm_visi');
		exec_query("insert into rpjm_visi (id_visi,visi,periode) values(".$maxid.",'".$visi."','".$periode."')");
		$stored = get_data('select id_visi from rpjm_visi where id_visi='.$maxid);
		if($stored['id_visi']==$maxid){
			echo 'success__';
			$idvisi = $maxid;
		}
		logs('Menambahkan Visi periode '.$periode,'Visi = '.$visi);
	}
	echo $idvisi.'__';
	view_visi();
	
}else if ($act == 'edit_visi'){
	$idvisi=$_POST['idvisi'];
	if(is_numeric($idvisi)){
		$data = get_data("select * from rpjm_visi where id_visi=".$idvisi);
		
		$ress = implode($data,'__');
		print_r($ress);
		
	}else{
		echo 'error';
	}
}	

function view_visi(){
	$t_visi = get_datas ("select * from rpjm_visi order by id_visi");
	$no=1;
	foreach ($t_visi as $visi){
	?>
                                	<tr>
                                    	<td style="text-align:center"><?php echo $no; ?></td>
                                        <td><?php echo $visi ['visi'];?></td>
                                    	<td style="text-align:center"><?php echo $visi['periode']; ?></td>
                                    	<td style="text-align:center"><a href="#collapseTwo" data-toggle="collapse" data-parent="#accordion2" class="btn btn-info btn-small bt-edit-visi" name="<?php echo $visi['id_visi']; ?>"><i class="icon-edit icon-white"></i>Ubah</a></td>
                                    </tr>
    <?php
		$no++;
	}
}
?>